package evocarcassonne.tileseteditor.managers;

import evocarcassonne.core.board.Tile;
import evocarcassonne.core.board.locations.Zone;
import evocarcassonne.core.utils.ZoneType;

import java.util.*;

public class TileManager {

    private static int index = 0;

    static Tile tile;
    static HashMap<Integer, Tile> tiles = new HashMap();

    public static void setType(int card, ZoneType type) {

        if (tile == null) {
            return;
        }

        tile.setCardType(card, type);
    }

    public static void addTile() {
        Tile tile = new Tile();
        tile.number = ++index;

        tiles.put(index, tile);
        setTile(index);
    }

    public static void addTile(int i) {
        Tile tile = new Tile();
        tile.number = i;

        tiles.put(i, tile);
        setTile(i);
    }

    public static void removeTile(int i) {
        tiles.remove(i);
    }

    public static void setTile(int i) {
        tile = tiles.get(i);
    }

    public static void resetLinks() {

        if (tile == null) {
            return;
        }

        for (int i = 0; i < 12; i++) {
            Zone curZone = tile.getZone(i);
            curZone.resetLinks();
        }
    }

    public static Tile getTile() {
        return tile;
    }

    public static void addLink(int... cards) {

        if (tile == null) {
            return;
        }

        List<Integer> list = new ArrayList();

        for (int card : cards) {
            Zone mainZone = tile.getZone(card);
            Set<Zone> links = mainZone.getLinks();

            for (Zone foreignZone : links) {
                list.add(foreignZone.card);
            }
        }

        for (int i : cards) {
            list.add(i);
        }

        tile.addLink(list);
    }

    public static void resetSet() {

        index = 0;

        tiles.clear();

    }
}
